#include "Library.h"

void Library::AddResource( const HashedString& name, Pointer<IResource>& resource )
{
	m_resources[resource->getResourceType()].insert(IResource::Pair_t(name, resource));
}

Pointer<IResource> Library::getResource( const HashedString& type, const HashedString& name ) const
{
	ResourcesMap_t::const_iterator resourceMap;
	
	if ((resourceMap = m_resources.find(type)) != m_resources.end()) // found type
	{
		IResource::Map_t::const_iterator resource;

		if ((resource = resourceMap->second.find(name)) != resourceMap->second.end())
		{
			return resource->second;
		}
	}
	return 0;
}

HashedString Library::getResourceName( HashedString type, IResource* resource ) const
{
	ResourcesMap_t::const_iterator resourceMap;
	
	if ((resourceMap = m_resources.find(type)) != m_resources.end()) // found type
	{
		for (IResource::Map_t::const_iterator it = resourceMap->second.begin(); it != resourceMap->second.end(); ++it)
		{
			IResource* ptr = it->second;
			if (resource == ptr)
				return it->first;
		}
	}
	return 0;
}

unsigned int Library::ResourceCount() const
{
	unsigned int count = 0;
	for (ResourcesMap_t::const_iterator it = m_resources.begin(); it != m_resources.end(); ++it)
	{
		count += it->second.size();
	}
	return count;
}

Library::ResourcesMap_t& Library::getResources()
{
	return m_resources;
}